package com.my.bbs.service.impl;

import com.my.bbs.dao.MyPostCollectMapper;
import com.my.bbs.dao.MyPostMapper;
import com.my.bbs.dao.MyUserMapper;
import com.my.bbs.entity.MyPost;
import com.my.bbs.entity.MyPostCollect;
import com.my.bbs.entity.MyUser;
import com.my.bbs.service.MyPostCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 86187
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MyPostCollectServiceImpl implements MyPostCollectService {

    @Autowired
    private MyPostCollectMapper myPostCollectMapper;

    @Autowired
    private MyPostMapper myPostMapper;

    @Autowired
    private MyUserMapper myUserMapper;

    @Override
    @Transactional
    public Boolean addCollectRecord(Long userId, Long postId) {

        MyPostCollect myPostCollect = myPostCollectMapper.selectByUserIdAndPostId(userId, postId);

        MyUser myUser = myUserMapper.selectByPrimaryKey(userId);

        if (myUser == null || myUser.getUserStatus().intValue() == 1) {
            //账号已被封禁
            return false;
        }

        if (myPostCollect != null) {
            return true;
        } else {
            myPostCollect = new MyPostCollect();
            myPostCollect.setPostId(postId);
            myPostCollect.setUserId(userId);

            //收藏数量加1
            MyPost myPost = myPostMapper.selectByPrimaryKey(postId);

            myPost.setPostCollects(myPost.getPostCollects() + 1);

            if (myPostCollectMapper.insertSelective(myPostCollect) > 0 && myPostMapper.updateByPrimaryKey(myPost) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional
    public Boolean deleteCollectRecord(Long userId, Long postId) {

        MyPostCollect myPostCollect = myPostCollectMapper.selectByUserIdAndPostId(userId, postId);

        MyUser myUser = myUserMapper.selectByPrimaryKey(userId);

        if (myUser == null || myUser.getUserStatus().intValue() == 1) {
            //账号已被封禁
            return false;
        }

        if (myPostCollect == null) {
            return true;
        } else {
            //收藏数量减1
            MyPost myPost = myPostMapper.selectByPrimaryKey(postId);

            Long collectCount = myPost.getPostCollects() - 1;
            if (collectCount >= 0) {
                myPost.setPostCollects(collectCount);
            }

            if (myPostCollectMapper.deleteByPrimaryKey(myPostCollect.getRecordId()) > 0 && myPostMapper.updateByPrimaryKey(myPost) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean validUserCollect(Long userId, Long postId) {
        MyPostCollect myPostCollect = myPostCollectMapper.selectByUserIdAndPostId(userId, postId);
        if (myPostCollect == null) {
            return false;
        }
        return true;
    }

    @Override
    public List<MyPost> getCollectRecordsByUserId(Long userId) {

        List<MyPostCollect> myPostCollects = myPostCollectMapper.listByUserId(userId);

        if (!CollectionUtils.isEmpty(myPostCollects)) {
            List<Long> postIds = myPostCollects.stream().map(MyPostCollect::getPostId).collect(Collectors.toList());
            List<MyPost> myPosts = myPostMapper.selectByPrimaryKeys(postIds);
            return myPosts;
        }

        return null;
    }
}
